from typing import Dict, Sequence

from schedule.PowerSchedule import PowerSchedule
from utils.Seed import Seed


class PathPowerSchedule(PowerSchedule):

    def __init__(self) -> None:
        super().__init__()
        # TODO
        # 路径频率字典，用于存储各路径的执行次数
        self.external_freq = None  # 外部频率引用

    def bind_frequency(self, freq_dict: Dict[int, int]):
        """绑定到Fuzzer的频率字典"""
        self.external_freq = freq_dict

    def assign_energy(self, population: Sequence[Seed]) -> None:
        """Assign exponential energy inversely proportional to path frequency"""
        # TODO
        if not self.external_freq:
            raise RuntimeError("Frequency dict not bound!")

        for seed in population:
            path_id = hash(frozenset(seed.coverage))
            freq = self.external_freq.get(path_id, 0)
            seed.energy = 1.0 / (freq + 1)
